package com.oem.qisda;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.oem.qisda.databinding.ActivityMainBinding;

import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    BikeAPI bikeAPI = new BikeAPI();

    private Handler handler = new Handler(Looper.getMainLooper());
    private String[] commands = {
            "520D2AD2640082099C0003005700E10A",
            "520D2AD2640084089C0003004E00D90A",
            "520D2AD264006D099E0003005700CE0A",
            "520D2AD2640012099E00030053006F0A",
            "520D2AD26400BC089C0003005000130A",
            "520D2AD264006109A60003005600C90A"
    };
    private int totalIterations = 1;  // 发送两轮
    private int currentIteration = 0;
    private int currentCommandIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  // 使用主布局

        // 获取 TextView 和 Button
        TextView resultTextView = findViewById(R.id.result_text_view);
        Button parseButton = findViewById(R.id.parse_button);

        // 设置按钮点击事件
        parseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 重置计数器
                currentIteration = 0;
                currentCommandIndex = 0;

                // 启动发送任务
                handler.postDelayed(sendCommandRunnable, 0);
            }
        });
    }

    // 任务：发送命令并调用 parseCommand
    private Runnable sendCommandRunnable = new Runnable() {
        @Override
        public void run() {
            if (currentIteration < totalIterations) {
                // 获取当前命令并调用 parseCommand
                String command = commands[currentCommandIndex];
                String result = bikeAPI.parseCommand(command);

                // 显示结果到 TextView
                TextView resultTextView = findViewById(R.id.result_text_view);
                resultTextView.setText("解析结果: " + result);

                // 显示 Toast 提示
                Toast.makeText(MainActivity.this, "解析完成：" + result, Toast.LENGTH_SHORT).show();

                // 日志输出
                Log.d("ParseResult", result);

                // 保存结果到文件
                saveResultToFile(result);

                // 更新命令索引
                currentCommandIndex++;

                // 如果所有命令发送完毕，进入下一轮
                if (currentCommandIndex >= commands.length) {
                    currentCommandIndex = 0;
                    currentIteration++;
                }

                // 每隔 1000ms 继续发送下一个命令
                handler.postDelayed(this, 1000);
            }
        }
    };

    // 保存结果到文件
    private void saveResultToFile(String result) {
        FileOutputStream fos = null;
        try {
            // 打开 save.bin 文件（如果文件不存在，将会自动创建）
            fos = openFileOutput("save.bin", MODE_APPEND);  // 使用 MODE_APPEND 追加写入
            fos.write((result + "\n").getBytes());  // 写入结果并换行
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}